<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Model;

use Think\Model\RelationModel;
use Xcx\Model\UsersModel;

/**
 * Class AnswerModel
 * @package Home\Model
 */
class AnswerModel extends RelationModel {

    public function __construct() {
        parent::__construct();
        $this->usersModel = new UsersModel();
        $this->user_id = session('user_id');
    }

    public function getInfoByQuestionId($id) {
        $id = $id ? $id : 0;
        $find = M('answer')->alias('a')->join('left join __ACCOUNTANT_USERS__ as au on a.accountant_id = au.au_id and au.status = 1')
            ->field('a.*,au.au_id,au.username,au.mobile,au.sex,au.head_pic')
            ->where('a.question_id = '.$id)->find();

        if($find){
            $user_id = M('question')->where('id = '.$id)->getField('user_id');
            $find['head_pic'] =  (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];
            $find['add_time'] = date('Y-m-d H:i:s',$find['add_time']);
            $find['adoption_num'] = $this->usersModel->getAdoptionNum($find['question_id'],$find['accountant_id']);
            if($find['charge_view'] && $user_id != $this->user_id){
                $is_pay = $this->usersModel->isPayByQuestionId($find['question_id'],$this->user_id, 3);
                if(!$is_pay){
                    $find['content'] = '';
                    $find['global_status'] = 2;
                }
            }
            return $find;
        }
        return false;
    }

    public function getUserByQuestionId($id) {
        $id = $id ? $id : 0;
        $find = M('answer')->alias('a')->join('left join __ACCOUNTANT_USERS__ as au on a.accountant_id = au.au_id and au.status = 1')
            ->field('au.username,au.mobile,au.sex,au.head_pic')
            ->where('a.question_id = '.$id)->find();

        if($find){
            $find['head_pic'] = (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];
            return $find;
        }
        return false;
    }

}
